package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyerCarBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.mapper.IBuyerCarMapper;
import com.project.mapper.IProductMapper;
import com.project.service.IBuyerCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuyerCarServiceImpl implements IBuyerCarService {
    @Autowired
    private IBuyerCarMapper buyerCarMapper;
    @Autowired
    private IProductMapper productMapper;
    /*-------------------------------------------------------------------------------------------------------*/

    /**
     * 通过登陆用户编号查询采购车信息，同时查询商品信息
     *
     * @param userId 登陆用户id
     * @return 采购车信息集合
     */
    @Override
    public List<BuyerCarBean> findByUserId(Integer userId) {
        System.out.println(buyerCarMapper.findByUserId(userId) + "1111111111111111");
        return buyerCarMapper.findByUserId(userId);
    }

    /**
     * 添加采购商品
     * 1.判断option的值是否为add，如果是，则完成库存商品添加。
     * 2.判断购物车中是否存在该商品，如果不存在，则新加采购车记录
     * 如果购物车中存在该商品，则修改采购数量
     *
     * @param option  操作
     * @param product 商品编号
     * @param num     商品数量
     * @param userId  登陆用户
     */
    @Override
    public void add(String option, ProductBean product, Integer num, Integer userId) {
//        System.out.println(product);
//        System.out.println(option);
//        System.out.println(num);
//        if (option.equals("add")) {
//            QueryWrapper<BuyerCarBean> qw = new QueryWrapper<>();
//            qw.eq("fk_productId", product.getId());
//            qw.eq("fk_userId", userId);
//            BuyerCarBean buyerCar = buyerCarMapper.selectOne(qw);
//            if (buyerCar == null) {
//                buyerCar = new BuyerCarBean(product.getId(), num, userId, product.getBuyMoney());
//                buyerCarMapper.insert(buyerCar);
//            } else {
//                buyerCar.setNum(buyerCar.getNum() + num);
//                buyerCarMapper.updateById(buyerCar);
//            }
//
//            QueryWrapper<ProductBean> pqw = new QueryWrapper<>();
//            pqw.eq("pk_productId", product.getId());
//            ProductBean productBean = productMapper.selectOne(pqw);
//            if (productBean == null) {
//                productBean.setNum(0);
//                productBean.setBuyMoney(null);
//                productBean.setSellMoney(null);
//                productMapper.insert(productBean);
//            }
//        }
        System.out.println(product.getId() + "============================");
        System.out.println(product + "============================");
        System.out.println(option);
        QueryWrapper<BuyerCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId", product.getId());
        qw.eq("fk_userId", userId);
        BuyerCarBean buyerCarBean = buyerCarMapper.selectOne(qw);
        if (buyerCarBean == null) {
            buyerCarBean = new BuyerCarBean(product.getId(), userId, product.getBuyMoney(), num);
            buyerCarMapper.insert(buyerCarBean);
        } else {
            buyerCarBean.setNum(num + buyerCarBean.getNum());
            buyerCarMapper.updateById(buyerCarBean);
        }
        if (option.equals("add")) {
            ProductBean productBean = product;
            productBean.setNum(0);
            productBean.setBuyMoney(0);
            productMapper.insert(productBean);
        }
    }

    /**
     * 按采购车id删除
     *
     * @param buyerCarId 采购车id
     */
    @Override
    public void del(Integer buyerCarId) {
        buyerCarMapper.deleteById(buyerCarId);
    }

    /**
     * 按采购车id修改数量
     *
     * @param buyerCarId 采购车id
     * @param num        数量
     */
    @Override
    public void update(Integer buyerCarId, Integer num) {
        BuyerCarBean buyerCar = new BuyerCarBean();
        buyerCar.setId(buyerCarId);
        buyerCar.setNum(num);
        buyerCarMapper.updateById(buyerCar);
    }
}
